<?php

namespace App\Morefun\Quiz;

use App\Contracts\Repositories\Quiz\OptionRepository as OptionRepositoryContact;
use App\Entities\QuizOption;
use Carbon\Carbon;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Facades\DB;

class OptionService
{
    private $options;

    public function __construct(OptionRepositoryContact $options)
    {
        $this->options = $options;
    }

    public function bind($activityId, array $option)
    {

    }

    public function batchBind($quizId, array $options)
    {
        if (empty($options)) {
            return true;
        }

        $now = Carbon::now();
        $i = [];
        $j = 1;
        $sec = 0;
        foreach ($options as $of => $option) {
            if (empty($option['text']) && empty($option['resource'])) {
                continue;
            }
            // 制造出添加的options的时间差
            $at = $now->addSecond($sec)->toDateTimeString();
            ++$sec;

            $i[$of]['id'] = Uuid::uuid4()->toString();
            $i[$of]['quiz_id'] = $quizId;
            $i[$of]['text'] = isset($option['text']) ? $option['text'] : '';
            $i[$of]['sort'] = (isset($option['sort']) && $option['sort'] > 0) ? $option['sort'] : $j;
            $i[$of]['resource'] = isset($option['resource']) ? $option['resource'] : '';
            $i[$of]['fillable'] = isset($option['fillable']) ? $option['fillable'] : 0;
            $i[$of]['fill_required'] = isset($option['fill_required']) ? $option['fill_required'] : 0;
            $i[$of]['created_at'] = $at;
            $i[$of]['updated_at'] = $at;
            $j++;
        }

        return QuizOption::query()->insert($i);
    }

    public function updateQuizOptions($id, $insert = [])
    {
        $updateRet = [];
        DB::beginTransaction();
        $this->options->deleteByQuizId($id);
        $insertRet = $this->multipleAdd($id, $insert);
        DB::commit();
        if (count($insertRet) != count($insert)) {
            return false;
        }

        return ($insertRet + $updateRet);
    }

    private function multipleAdd($id, $insert)
    {
        $result = [];
        if (!empty($insert)) {
            $sort = 0;
            $sortDB = $this->options->findMAXSortByQuizId($id);
            if (!empty($sortDB)) {
                $sort = $sortDB;
            }
            foreach ($insert as $index => $item) {
                $option = $this->options->findTextByQuizId($item['quiz_id'], $item['text']);
                if ($option) {
                    $result[$option->text] = $option->id;
                    continue;
                }
                $sort++;
                $item['sort'] = $sort;
                $id = $this->options->create($item);
                $result[$item['text']] = $id;
            }

            return $result;
        }

        return $result;
    }

    private function multipleUpdate($update)
    {
        $result = [];
        if (!empty($update)) {
            foreach ($update as $item) {
                $this->options->update($item['id'], $item);
                $result[$item['text']] = $item['id'];
            }

            return $result;
        }

        return $result;
    }

}
